//
//  NewsController.m
//  oschina_song
//
//  Created by song on 16/1/25.
//  Copyright © 2016年 song. All rights reserved.
//

#import "NewsController.h"
#import "UIColor+utils.h"
#import "OSCNews.h"
#import "NewsCell.h"
#import "OSCNewsDetails.h"
#import "DetailsViewController.h"
#import "Utils.h"

static NSString *kNewsCellID =@"NewsCell";

@interface NewsController ()

@property(nonatomic, assign) NewsListType type;



@end

@implementation NewsController



-(id)initWithNewsListType:(NewsListType)type
{
    self = [super init];
    if(self){
            _type = type;
        __weak NewsController *weakSelf = self;
        self.objclass = [OSCNews class];
        self.generateURL= ^NSString *(NSUInteger page)
        {
            if(type < 4)
            {
                return  [NSString stringWithFormat:@"%@%@?catalog=%d&pageIndex=%lu&%@",OSCAPI_PREFIX,OSCAPI_NEWS_LIST, type,(unsigned long)page,OSCAPI_SUFFIX];
            }else{
                 return [NSString stringWithFormat:@"%@%@?show=month", OSCAPI_PREFIX, OSCAPI_NEWS_LIST];
            }
        };

        [self.tableView registerClass:[NewsCell class] forCellReuseIdentifier:kNewsCellID];
        
    }
    return self;
}

-(NSArray *)parseXML:(ONOXMLDocument *)xml
{
    return [[xml.rootElement firstChildWithTag:@"newslist"] childrenWithTag:@"news"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    NewsCell *cell =[tableView dequeueReusableCellWithIdentifier:kNewsCellID forIndexPath:indexPath];
    
    OSCNews *news = self.objects[indexPath.row];
    
    [cell.titleLable setText:news.title];
    [cell.bodyLabel setText:news.body];
    [cell.authorLabel setText:news.author];
    [cell.timeLabel setText:@"2016年1月23"];
    [cell.commentCount setText:@"23"];
//    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
//    cell.selectedBackgroundView.backgroundColor = [UIColor blueColor];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OSCNews *news = self.objects[indexPath.row];
    
    self.label.font = [UIFont boldSystemFontOfSize:15];
    self.label.text =news.title;
    CGFloat height =[self.label sizeThatFits:CGSizeMake(tableView.frame.size.width -16, MAXFLOAT)].height;
    
    self.label.font = [UIFont systemFontOfSize:13];
     self.label.text = news.body;
    
    height += [self.label sizeThatFits:CGSizeMake(tableView.frame.size.width -16, MAXFLOAT)].height;
    
    return height + 42;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OSCNews *news = self.objects[indexPath.row];
    if(news.url.absoluteString.length>0)
    {
        [Utils analysis:news.url.absoluteString andNavController:self.navigationController];
    }else
    {
        DetailsViewController *detailVC =[[DetailsViewController alloc] initWithNews:news];
        [self.navigationController pushViewController:detailVC animated:YES];
    }

}

@end
